<?php
/**
 * CakeCMS - 系统公共方法
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */

// 定义项目上传文件目录
define ( 'UPLOAD_PATH', ROOT . DS . 'www' . DS . 'webroot' . DS . 'files' . DS );

// 定义TMP文件上传目录，每天会清理这个目录
define ( 'UPLOAD_PATH_TMP', ROOT . DS . 'www' . DS . 'webroot' . DS . 'files' . DS . 'tmp' . DS );

/**
 * 产生一个随机字符串
 *
 * @param int $len        	
 * @return string
 */
function generateRand($len) {
	$c = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	$rand = '';
	
	srand ( ( double ) microtime () * 1000000 );
	for($i = 0; $i < $len; $i ++) {
		$rand .= $c [rand () % strlen ( $c )];
	}
	return $rand;
}

/**
 * 对数据进行编码转换
 *
 * @param $data array/string
 *        	数组
 * @param $input string
 *        	需要转换的编码
 * @param $output string
 *        	转换后的编码
 */
function array_iconv($data, $input = 'gbk', $output = 'utf-8') {
	if (! is_array ( $data )) {
		return iconv ( $input, $output, $data );
	} else {
		foreach ( $data as $key => $val ) {
			if (is_array ( $val )) {
				$data [$key] = array_iconv ( $val, $input, $output );
			} else {
				$data [$key] = iconv ( $input, $output, $val );
			}
		}
		return $data;
	}
}

/**
 * 建立目录
 */
function makeDir($path_info) {
	$path = UPLOAD_PATH . $path_info . DIRECTORY_SEPARATOR;
	$path_mk = null;
	if (file_exists ( $path )) {
		return false;
	} else {
		$path_arr = explode ( DIRECTORY_SEPARATOR, $path_info );
		foreach ( $path_arr as $value ) {
			$path_mk .= $value . DIRECTORY_SEPARATOR;
			if (! file_exists ( $path_mk ))
				@mkdir ( $path_mk );
		}
		return true;
	}
}

/**
 * HTTP方式数据传输
 *
 * @param $url URL地址        	
 * @param $data 如果是POST方式赋
 *        	$_POST 值
 * @return 取得到的结果集
 */
function http_to_host($url, $data = '', $http_header = null) {
	$url = parse_url ( $url );
	if (! $url) {
		return false;
	}
	if (! isset ( $url ['port'] )) {
		$url ['port'] = "";
	}
	if (! isset ( $url ['query'] )) {
		$url ['query'] = "";
	}
	
	$encoded = "";
	
	if (is_array ( $data )) {
		while ( list ( $k, $v ) = each ( $data ) ) {
			$encoded .= ($encoded ? "&" : "");
			$encoded .= rawurlencode ( $k ) . "=" . rawurlencode ( $v );
		}
		$type = 'post';
	}
	
	$fp = @fsockopen ( $url ['host'], $url ['port'] ? $url ['port'] : 80, $errno, $errstr, 15 );
	if (! $fp) {
		@fclose ( $fp );
		return false;
	}
	
	if ($type == 'post') {
		fputs ( $fp, sprintf ( "POST %s%s%s HTTP/1.0\n", $url ['path'], $url ['query'] ? "?" : "", $url ['query'] ) );
		fputs ( $fp, "Host: $url[host]\n" );
		fputs ( $fp, "Content-type: application/x-www-form-urlencoded\n" );
		fputs ( $fp, "Content-length: " . strlen ( $encoded ) . "\n" );
		if (is_array ( $http_header )) {
			foreach ( $http_header as $key => $value ) {
				fputs ( $fp, $key . ": " . $value );
			}
		}
		
		fputs ( $fp, "Connection: close\n\n" );
	} else {
		fputs ( $fp, sprintf ( "GET %s%s%s HTTP/1.1\n", $url ['path'], $url ['query'] ? "?" : "", $url ['query'] ) );
		fputs ( $fp, "Host: $url[host]\n" );
		if (is_array ( $http_header )) {
			foreach ( $http_header as $key => $value ) {
				fputs ( $fp, $key . ": " . $value );
			}
		}
		fputs ( $fp, "Connection: Close\n\n" );
	}
	
	fputs ( $fp, "$encoded\n" );
	
	// $line = fgets($fp,1024);
	// if (!eregi("^HTTP/1\.. 200", $line)) return;
	
	$results = "";
	$inheader = 1;
	while ( ! feof ( $fp ) ) {
		$line = @fgets ( $fp, 1024 );
		// dump($line);
		if ($inheader && ($line == "\n" || $line == "\r\n")) {
			$inheader = 0;
		} elseif (! $inheader) {
			$results .= $line;
		}
	}
	@fclose ( $fp );
	
	return $results;
}

/*
 * 截取字符串 @param $string 要截取的字符串 @param $length 截取长度 @param $dot @return 取得到的结果集
 */
function cutstr($string, $length, $dot = ' ...') {
	$charset = 'utf-8';
	if (strlen ( $string ) <= $length) {
		return $string;
	}
	$strcut = '';
	if (strtolower ( $charset ) == 'utf-8') {
		$n = $tn = $noc = 0;
		while ( $n < strlen ( $string ) ) {
			
			$t = ord ( $string [$n] );
			if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
				$tn = 1;
				$n ++;
				$noc ++;
			} elseif (194 <= $t && $t <= 223) {
				$tn = 2;
				$n += 2;
				$noc += 2;
			} elseif (224 <= $t && $t < 239) {
				$tn = 3;
				$n += 3;
				$noc += 2;
			} elseif (240 <= $t && $t <= 247) {
				$tn = 4;
				$n += 4;
				$noc += 2;
			} elseif (248 <= $t && $t <= 251) {
				$tn = 5;
				$n += 5;
				$noc += 2;
			} elseif ($t == 252 || $t == 253) {
				$tn = 6;
				$n += 6;
				$noc += 2;
			} else {
				$n ++;
			}
			
			if ($noc >= $length) {
				break;
			}
		}
		if ($noc > $length) {
			$n -= $tn;
		}
		
		$strcut = substr ( $string, 0, $n );
	} else {
		for($i = 0; $i < $length - 3; $i ++) {
			$strcut .= ord ( $string [$i] ) > 127 ? $string [$i] . $string [++ $i] : $string [$i];
		}
	}
	
	return $strcut . $dot;
}

/**
 * 转换字节数为其他单位
 *
 *
 * @param $filesize string        	
 * @return string
 */
function sizecount($filesize) {
	if ($filesize >= 1073741824) {
		$filesize = round ( $filesize / 1073741824 * 100 ) / 100 . ' GB';
	} elseif ($filesize >= 1048576) {
		$filesize = round ( $filesize / 1048576 * 100 ) / 100 . ' MB';
	} elseif ($filesize >= 1024) {
		$filesize = round ( $filesize / 1024 * 100 ) / 100 . ' KB';
	} else {
		$filesize = $filesize . ' Bytes';
	}
	return $filesize;
}
?>